package com.example.myapplication.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.example.myapplication.R;
import com.github.chrisbanes.photoview.PhotoView;

/**
 * created by moxun on 2022/5/6
 * moxun@zhuangdianwenhua.com
 */
public class PreviewFragment extends Fragment {
    private PhotoView ivImage;
    private static final String EXTRA_DATA = "extra_data";
    private static final String EXTRA_CURRENT_POSITION = "extra_current_position";
    private static final String EXTRA_START_POSITION = "extra_start_position";

    private String url;
    private int currentPosition;
    private int startPosition;

    public static PreviewFragment newInstance(String url, int currentPosition, int startPosition) {
        PreviewFragment previewFragment = new PreviewFragment();
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_DATA, url);
        bundle.putInt(EXTRA_CURRENT_POSITION, currentPosition);
        bundle.putInt(EXTRA_START_POSITION, startPosition);
        previewFragment.setArguments(bundle);
        return previewFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_preview, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ivImage = view.findViewById(R.id.iv_image);
        if (getArguments() != null) {
            url = getArguments().getString(EXTRA_DATA);
            currentPosition = getArguments().getInt(EXTRA_CURRENT_POSITION);
            startPosition = getArguments().getInt(EXTRA_START_POSITION);

        }
//        ImageLoader.show(ImageLoader.Builder.withString(this, url)
//                .noPlaceholderAndError()
//                .listener(new RequestListener<Drawable>() {
//                    @Override
//                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                        enterTransition();
//                        return false;
//                    }
//
//                    @Override
//                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                        enterTransition();
//                        return false;
//                    }
//                })
//                .build(), ivImage);

        Glide.with(this)
                .load(url)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        enterTransition();
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        enterTransition();
                        return false;
                    }
                })
                .apply(new RequestOptions().dontTransform())
                .into(ivImage);
        ivImage.setTransitionName(url);
    }

    @Override
    public void onResume() {
        super.onResume();


    }

    private void enterTransition() {
        if (currentPosition != startPosition) {
            return;
        }
        if (getActivity() == null) {
            return;
        }
//        View decorView = getActivity().getWindow().getDecorView();
        ivImage.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                ivImage.getViewTreeObserver().removeOnPreDrawListener(this);
                if (getActivity() != null && !getActivity().isFinishing()) {
                    getActivity().startPostponedEnterTransition();
                }
                return true;
            }
        });
    }


}
